//
//  UserListViewController.m
//  BiologyLibrary
//
//  Created by 朱进 on 16/7/2.
//  Copyright © 2016年 youxh. All rights reserved.
//

#import "UserListViewController.h"

@implementation UserListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNaviBarTitle:@"选择用户"];
    // 创建一个自定义的按钮，并添加到导航条左侧。
    UIButton *btnNaviLeft = [CustomNaviBarView createImgNaviBarBtnByImgNormal:@"bar_left_image"
                                                                 imgHighlight:@"bar_left_image_f"
                                                                       target:self
                                                                       action:@selector(btnNaviLeftClick)];
    [self setNaviBarLeftBtn:btnNaviLeft];

    _contentArray = [User findAll];

    _contentTableView = [[UITableView alloc]initWithFrame:
                         CGRectMake(0, 64, fWidth_Current,
                                    fHeight_Current - fHeight_StatueBar)
                                                    style:0];
    _contentTableView.backgroundColor = [UIColor clearColor];
    _contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _contentTableView.dataSource = self;
    _contentTableView.delegate = self;
    [self.view addSubview:_contentTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)btnNaviLeftClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_contentArray count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *strIdentifier = @"strIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                             reuseIdentifier:strIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    cell.backgroundColor=[UIColor colorWithRed:0.12 green:0.14 blue:0.18 alpha:1.0];
    User *tempUser = (User *)[_contentArray objectAtIndex:indexPath.row];
    UILabel*namelab=[[UILabel alloc]initWithFrame:CGRectMake(60, 0, fWidth_Current-100, 50)];
    namelab.text = tempUser.name;
    namelab.textColor=[UIColor whiteColor];
    [cell addSubview:namelab];
    UIImageView*userheadview=[[UIImageView alloc]initWithFrame:CGRectMake(10, 6, 38, 38)];
    userheadview.layer.cornerRadius=19;
    userheadview.clipsToBounds=YES;
    userheadview.image=[UIImage imageNamed:@"userhead_temp"];
    [cell addSubview:userheadview];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self dismissViewControllerAnimated:YES completion:^{
        User *tempUser = (User *)[_contentArray objectAtIndex:indexPath.row];
        if(self.delegate && [self.delegate respondsToSelector:@selector(userListDidSelectedUser:)]){
            [self.delegate userListDidSelectedUser:tempUser];
        }
    }];
}

@end
